package com.ocom.webapi.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ocom.common.constant.WebApiServiceUrl;
import com.ocom.common.dto.Department;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.web.PersonnelAreaRequest;
import com.ocom.common.request.wx.UserAccessPageSetAccessRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.utils.StringUtils;
import com.ocom.common.vo.access.PersonnelAreaVo;
import com.ocom.security.authentication.YoCiUser;
import com.ocom.security.utils.SecurityUtils;
import com.ocom.webapi.feign.AccessClientService;
import com.ocom.webapi.feign.HumanClientService;
import com.ocom.webapi.feign.MqSimpleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@PreAuthorize("@pms.hasRole('ROLE_WEB')" )
public class PersonnelAreaController {

    @Autowired
    private AccessClientService accessClientService;

    @Autowired
    private HumanClientService humanClientService;

    @Autowired
    private MqSimpleService mqSimpleService;

    //权限组列表
    @PostMapping(WebApiServiceUrl.URL_AreaAccessPageAccessList)
    public Result merchPageGetFkConfig(@RequestBody @Validated PersonnelAreaRequest request){
        if (request.getSubDept()){
            Department department=new Department();
            department.setComId(request.getComId());
            department.setDeptId(Long.valueOf(request.getDeptId()));
            department.setShowLevel(0);
            Result bootparentDeptId=humanClientService.getDeptIds(department);
            if (bootparentDeptId.getCode()==200){
                JSONObject bootparentDeptIdObject= JSON.parseObject(JSON.toJSONString(bootparentDeptId.getData()));
                String deptIds="";
                if (bootparentDeptIdObject.getString("deptIds").equals("")){
                    deptIds= String.valueOf(request.getDeptId());
                }else {
                    deptIds=request.getDeptId()+","+bootparentDeptIdObject.getString("deptIds");
                }
                request.setDeptIds(deptIds);
            }else {
                request.setDeptIds(String.valueOf(request.getDeptId()));
            }
            return accessClientService.getAreaAccessList(request);
        }else {
            if (request.getDeptId()==0){
                request.setDeptIds("");
            }else {
                request.setDeptIds(String.valueOf(request.getDeptId()));
            }
            return accessClientService.getAreaAccessList(request);
        }
    }

    //更改权限组
    @PostMapping(WebApiServiceUrl.URL_AreaAccessChangeAccess)
    public Result areaAccessChangeAccess(@RequestBody @Validated PersonnelAreaRequest request){

        request.setPage(-1);
        request.setSize(-1);
        if (request.getGroupNo()==null){
            return ResultUtil.error(ResultEnum.Err_203);
        }

        //1、如果pId不为空：
        //		遍历pId，调用mq-simple changeAccess
        YoCiUser yoCiUser= SecurityUtils.getYoCiUser();
        if (StringUtils.isEmpty(request.getPId())){
            Result result=null;
            if (request.getSubDept()){
                Department department=new Department();
                department.setComId(request.getComId());
                department.setDeptId(Long.valueOf(request.getDeptId()));
                department.setShowLevel(0);
                Result bootparentDeptId=humanClientService.getDeptIds(department);
                if (bootparentDeptId.getCode()==200){
                    JSONObject bootparentDeptIdObject= JSON.parseObject(JSON.toJSONString(bootparentDeptId.getData()));
                    String deptIds="";
                    if (bootparentDeptIdObject.getString("deptIds").equals("")){
                        deptIds= String.valueOf(request.getDeptId());
                    }else {
                        deptIds=request.getDeptId()+","+bootparentDeptIdObject.getString("deptIds");
                    }
                    request.setDeptIds(deptIds);
                }else {
                    request.setDeptIds(String.valueOf(request.getDeptId()));
                }
                result = accessClientService.getAreaAccessList(request);
            }else {
                if (request.getDeptId()==0){
                    request.setDeptIds("");
                }else {
                    request.setDeptIds(String.valueOf(request.getDeptId()));
                }
                result = accessClientService.getAreaAccessList(request);
            }

            if (result.getCode() !=200){
                return result;
            }

            Map<String,Object> map=JSON.parseObject(JSON.toJSONString(result.getData()),Map.class);

            List<PersonnelAreaVo> list=JSON.parseObject(JSON.toJSONString(map.get("accessList")),List.class);
            for (PersonnelAreaVo personnelAreaVo : list) {
                UserAccessPageSetAccessRequest userAccess=new UserAccessPageSetAccessRequest();
                userAccess.setCompanyId(request.getComId());
                userAccess.setPId(yoCiUser.getId());
                userAccess.setUserId(Long.valueOf(personnelAreaVo.getPId()));
                userAccess.setAreaNo(Long.valueOf(request.getAreaNo()));
                userAccess.setRoleI(Long.valueOf(request.getGroupNo()));
                userAccess.setRegionId(0l);
//                mqSimpleService.ChangeAccess(userAccess);
                accessClientService.setAccess(userAccess);
            }
            return ResultUtil.success();
        }else {
            String[] arr=request.getPId().split(",");
            for (String s : arr) {
                UserAccessPageSetAccessRequest userAccess=new UserAccessPageSetAccessRequest();
                userAccess.setCompanyId(request.getComId());
                userAccess.setPId(yoCiUser.getId());
                userAccess.setUserId(Long.valueOf(s));
                userAccess.setAreaNo(Long.valueOf(request.getAreaNo()));
                userAccess.setRoleI(Long.valueOf(request.getGroupNo()));
                userAccess.setRegionId(0l);
//                mqSimpleService.ChangeAccess(userAccess);
                accessClientService.setAccess(userAccess);
            }
            return ResultUtil.success();
        }
    }
}
